#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    printf("mian 进程(%d)开始执行\n", getpid());

    pid_t pid = fork();

    if (pid == 0)   // 子进程
    {
        printf("子进程(%d)开始执行\n", getpid());

        // 通过execve()函数执行另一个程序
        char *path = "/bin/ping";
        char *argv[] = {path, "www.baidu.com", "-c", "5", NULL};
        char *envp[] = {NULL};
        execve(path, argv, envp);
        printf("execve error\n");
    }else if (pid > 0)  // 父进程
    {
        printf("父进程(%d)开始执行\n", getpid());

        int status;
        waitpid(pid, &status, 0);
        printf("子进程(%d)结束执行,回到父进程继续执行\n", status);
    }
    
    printf("进程(%d)结束执行\n", getpid());

    exit(EXIT_SUCCESS);
}
